package com.lms.thread.demo2;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-01-19 09:32
 * @Description: ${Description}
 */
public class SynchronizedTest {
    private Integer num = 0;
    private Integer syncNum = 0;

    public void nosync() {
        System.out.println(Thread.currentThread().getName() + ":begin");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        num++;
        System.out.println("线程：" + Thread.currentThread().getName() + ",num:" + num);
        System.out.println(Thread.currentThread().getName() + ":end");

    }

    public synchronized void sync() {
        System.out.println(Thread.currentThread().getName() + ":begin");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncNum++;
        System.out.println("线程：" + Thread.currentThread().getName() + ",syncNum:" + syncNum);
        System.out.println(Thread.currentThread().getName() + ":end");
    }

    public synchronized void sync2() {
        System.out.println(Thread.currentThread().getName() + ":begin");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncNum++;
        System.out.println("线程：" + Thread.currentThread().getName() + ",syncNum:" + syncNum);
        System.out.println(Thread.currentThread().getName() + ":end");
    }

    public void sync3() {
        synchronized ("aa") {
            System.out.println(Thread.currentThread().getName() + ":begin");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            syncNum++;
            System.out.println("线程：" + Thread.currentThread().getName() + ",syncNum:" + syncNum);
            System.out.println(Thread.currentThread().getName() + ":end");
        }
    }
}
